$(function () {
    // "text!com/contact.ejs"
    require([],function () {
        var parent = $(".parent");
        var name = $(".name");

        var partner = $(".partner");
        var born = $(".born");
        var death = $(".death");
        var src = $("#photo");
        var desc = $(".desc");
        var level = $(".level");
        var minm = $(".minm");
        var order = $(".order");
        $(".btn").on("click",function () {
            if(name.val() == ''){
                alert("姓名不能为空");
                return ;
            }
            var gender  = $(".gender:checked");

               $.ajax({
                   type:'get',
                   url:'/api/add',
                   data:{
                       parent:parent.val() || -1,
                       name:name.val(),
                       gender:gender.val() || '男',
                       partner:partner.val(),
                       born:born.val(),
                       death:death.val(),
                       src:src.data("src") || '',
                       desc:desc.val(),
                       level:level.val(),
                       minm:minm.val(),
                       order:order.val()
                   },
                   success:function (res) {
                       console.log(res);
                       window.location.reload();
                   }
               })
        });


        //发送图片
        $("#imgsend").on('change',function(){
            var that = this;
            $(that).uploadFile({
                url:"/api/file",
                data:{
                    old: src.data("src") || ''
                },
                filter:[],  //后缀文件筛选
                sendBefore:function(files){
                    //开始之前
                    console.log(files);
                },
                success:function(data,index){
                    //某个文件传完
                    src.attr('src',data.data);
                    src.data("src",data.data);
                },
                error:function(err,index){
                    //某个文件报错
                },
                progress:function(file){
                    //某个文件的上传进度

                    // file.loaded  已经上传的
                    // flie.total  总量
                    // file.percent  百分比
                    // file.index   第多少个文件

                }
            });
        });


        //文件大小格式化
        function FormatSize (fileSize) {
            var arrUnit = ["B", "K", "M", "G", "T", "P"],
                baseStep = 1024,
                unitCount = arrUnit.length,
                unitIndex = 0;
            while(fileSize >= baseStep && unitIndex < unitCount - 1){
                unitIndex++;
                fileSize /= baseStep;
            }
            fileSize = fileSize.toFixed(2);
            return fileSize + " " + arrUnit[unitIndex];
        }

    });
});